package com.hcw.library.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hcw.library.common.Result;
import com.hcw.library.dao.BookMapper;
import com.hcw.library.domain.Book;
import com.hcw.library.service.BookService;
import com.hcw.library.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper,Book> implements BookService {
    @Autowired
    private BookMapper bookMapper;

    @Override
    public Result<Integer> addBook(Book book) {
        Result<Integer> result = new Result<>();
        int row = bookMapper.insert(book);
        return result.defaultSuccess(row);
    }

    @Override
    public Result<Integer> deleteBook(String id) {
        Result<Integer> result = new Result<>();
        int row = bookMapper.deleteById(id);
        return result.defaultSuccess(row);
    }

    @Override
    public Result<Integer> updateBook(Book book) {
        Result<Integer> result = new Result<>();
        int row = bookMapper.updateById(book);
        return result.defaultSuccess(row);
    }

    @Override
    public Result<List<BookVO>> queryBookList(Integer currentPage) {
        Result<List<BookVO>> result = new Result<>();
        currentPage = currentPage == null ? 1 :currentPage;
        IPage<BookVO> page = new Page<>(currentPage,10);
        bookMapper.queryBookVOList(page);
        result.setTotalPage(page.getPages());
        result.setTotalRecord(page.getTotal());
        return result.defaultSuccess(page.getRecords());
    }


}
